package com.heihx.shop.service.Impl;

import com.heihx.api.client.MealClient;
import com.heihx.api.client.OrderClient;
import com.heihx.api.client.UserClient;
import com.heihx.constant.StatusConstant;
import com.heihx.entity.Orders;
import com.heihx.shop.service.WorkSpaceService;
import com.heihx.vo.BusinessDataVO;
import com.heihx.vo.DishOverViewVO;
import com.heihx.vo.OrderOverViewVO;
import com.heihx.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderClient orderClient;

    @Autowired
    private MealClient mealClient;

    @Autowired
    private UserClient userClient;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        //总订单数
        Long totalOrderCount = orderClient.countOrders(begin, end,null).getData();

        //营业额
        BigDecimal turnover = orderClient.sumOrders(begin, end, Orders.COMPLETED).getData();

        //有效订单数
        BigDecimal validOrderCount = BigDecimal.valueOf(orderClient.countOrders(begin, end, Orders.COMPLETED).getData());

        BigDecimal unitPrice = BigDecimal.valueOf(0.0);

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount.intValue() != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover.divide(validOrderCount,2, RoundingMode.HALF_UP);
        }

        //新增用户数
        Long newUsers = userClient.countByMap(begin, end).getData();

        return BusinessDataVO.builder()
                .turnover(turnover.doubleValue())
                .validOrderCount(validOrderCount.intValue())
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice.doubleValue())
                .newUsers(newUsers.intValue())
                .build();
    }

    @Override
    public OrderOverViewVO getOrderOverView() {
        //待接单
        Integer waitingOrders = Math.toIntExact(orderClient.countOrders(null,null,Orders.TO_BE_CONFIRMED).getData());

        //待派送
        Integer deliveredOrders = Math.toIntExact(orderClient.countOrders(null,null,Orders.CONFIRMED).getData());

        //已完成
        Integer completedOrders = Math.toIntExact(orderClient.countOrders(null,null,Orders.COMPLETED).getData());

        //已取消
        Integer cancelledOrders = Math.toIntExact(orderClient.countOrders(null,null,Orders.CANCELLED).getData());

        //全部订单
        Integer allOrders = Math.toIntExact(orderClient.countOrders(null,null,null).getData());

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        Integer sold = Math.toIntExact(mealClient.countDish(StatusConstant.ENABLE).getData());

        Integer discontinued = Math.toIntExact(mealClient.countDish(StatusConstant.DISABLE).getData());

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Integer sold = Math.toIntExact(mealClient.countSetMeal(StatusConstant.ENABLE).getData());

        Integer discontinued = Math.toIntExact(mealClient.countSetMeal(StatusConstant.DISABLE).getData());

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
